package com.manli.api.service;

import com.manli.api.base.exception.MyException;
import com.manli.api.dto.ViewUser;
import com.manli.api.util.QueryParamsMapUtil;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.pojo.SessionInfo;
import com.manli.api.util.ResultUtil;
import com.manli.api.util.SessionInfoUtils;

import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.manli.api.dao.RoleDao;
import com.manli.api.dto.Role;
import org.springframework.transaction.annotation.Transactional;


@Service("roleService")
public class RoleService {
    @Autowired
    RoleDao roleDao;
    @Autowired
    SessionInfoUtils sessionInfoUtils;
    @Autowired
    SQLManager sqlManager;
    
    public Result queryPage(Map<String, Object> params,HttpServletRequest request) {
    	SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        /*PageQuery<Role> page = roleDao.createQuery()
                .appendSql(" where 1=1 ")
                .appendSql(QueryParamsMapUtil.paramIsEmpty(params.get("status"))?"":" and status = " + params.get("status")+" ")
                .appendSql(QueryParamsMapUtil.paramIsEmpty(params.get("roleName"))?"":" and role_name like '%" + params.get("roleName") + "%'")
                .appendSql(sessionInfo.getBranchId() == 1? "":" and branch_id = " + sessionInfo.getBranchId() +" " )
                .page(Long.parseLong(params.get("page").toString()),Long.parseLong(params.get("limit").toString()));
        return ResultUtil.success(page.getList(),page.getTotalRow());*/


        StringBuffer sql =
                new StringBuffer("select r.*,b.branch_name as branchName from role r left join branch b on r.branch_id = b.branch_id ");
        sql.append(" where 1=1 ")
        .append(QueryParamsMapUtil.paramIsEmpty(params.get("status"))?"":" and status = " + params.get("status")+" ")
        .append(QueryParamsMapUtil.paramIsEmpty(params.get("roleName"))?"":" and role_name like '%" + params.get("roleName") + "%'")
        .append(sessionInfo.getBranchId() == 1? "":" and branch_id = " + sessionInfo.getBranchId() +" " );
        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<Role> pageQuery = new PageQuery<Role>(Long.parseLong(params.get("page").toString()),Long.parseLong(params.get("limit").toString()));
        PageQuery<Role> query =sqlManager.execute(sqlReady,Role.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<Role> list = roleDao.execute("select * from role where status=1");
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer roleId) {
        Role role = roleDao.single(roleId);
        return ResultUtil.success(role);
    }

    @Transactional
    public Result save(Role role) {
        if(role.getBranchId() == null){
            throw new MyException(-1,"机构为空");
        }
        if(StringUtils.isEmpty(role.getRoleName())){
            throw new MyException(-1,"角色名为空");
        }

        role.setStatus(1);
        roleDao.insert(role);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(Role role) {
        if(role.getBranchId() == null){
            throw new MyException(-1,"机构为空");
        }
        if(StringUtils.isEmpty(role.getRoleName())){
            throw new MyException(-1,"角色名为空");
        }

        roleDao.updateTemplateById(role);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeById(Integer roleId) {
        roleDao.deleteById(roleId);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            Role role = new Role();
            role.setStatus(0);
            role.setRoleId(id);
            roleDao.updateTemplateById(role);
        }
        return ResultUtil.success();
    }



}
